package com.wish.group05.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wish.group05.base.entity.Album;
import com.wish.group05.base.entity.PremisesFacility;
import com.wish.group05.base.entity.PremisesInfo;
import com.wish.group05.base.entity.RelationPremisesProperty;
import com.wish.group05.base.mapper.PremisesInfoMapper;
import com.wish.group05.base.service.IAlbumService;
import com.wish.group05.base.service.IPremisesFacilityService;
import com.wish.group05.base.service.IPremisesInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wish.group05.base.service.IRelationPremisesPropertyService;
import com.wish.group05.common.core.utils.StringUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 楼盘信息表 服务实现类
 * </p>
 *
 * @author houzehui
 * @since 2022-05-24
 */

@Service
public class PremisesInfoServiceImpl extends ServiceImpl<PremisesInfoMapper, PremisesInfo> implements IPremisesInfoService {
    @Autowired
    IPremisesInfoService iPremisesInfoService;
    @Autowired
    IRelationPremisesPropertyService relationPremisesPropertyService;
    @Autowired
    IPremisesFacilityService iPremisesFacilityService;
    @Autowired
    IAlbumService iAlbumService;

    //楼盘添加和修改
    @Override
    public boolean addtbPremisesInfo(PremisesInfo tbPremisesInfo) {
        //主表
        iPremisesInfoService.saveOrUpdate(tbPremisesInfo);
        //楼盘特色
        if (tbPremisesInfo.getConfigProperty().size()>=0){
            relationPremisesPropertyService.remove(new QueryWrapper<RelationPremisesProperty>().eq("premises_id",tbPremisesInfo.getId()));
            tbPremisesInfo.getConfigProperty().forEach(a->{
                RelationPremisesProperty property = new RelationPremisesProperty().setPremisesId(tbPremisesInfo.getId()).setPropertyId(a);
                relationPremisesPropertyService.saveOrUpdate(property);
            });
        }
        //追加行处理iPremisesFacilityService
        // 周边配套
        if (tbPremisesInfo.getPeripheralFacilities().size()>=0){
            iPremisesFacilityService.remove(new QueryWrapper<PremisesFacility>().eq("premises_id",tbPremisesInfo.getId()).eq("type",0));
            tbPremisesInfo.getPeripheralFacilities().forEach(a->{
                //0 是周边配套 1是交通情况
                PremisesFacility premisesFacility = new PremisesFacility().setPremisesId(tbPremisesInfo.getId()).setType(0).setTitle(a.getTitle()).setContent(a.getContent());
                iPremisesFacilityService.saveOrUpdate(premisesFacility);
            });
        }
        // 交通情况 List<PremisesFacility> trafficCondition;
        if (tbPremisesInfo.getTrafficCondition().size()>=0){
            iPremisesFacilityService.remove(new QueryWrapper<PremisesFacility>().eq("premises_id",tbPremisesInfo.getId()).eq("type",1));
            tbPremisesInfo.getPeripheralFacilities().forEach(a->{
                PremisesFacility premisesFacility = new PremisesFacility().setPremisesId(tbPremisesInfo.getId()).setType(1).setTitle(a.getTitle()).setContent(a.getContent());
                iPremisesFacilityService.saveOrUpdate(premisesFacility);
            });
        }
        //相册
        if (CollectionUtils.isNotEmpty(tbPremisesInfo.getAlbumList())) {
            iAlbumService.remove(new QueryWrapper<Album>().eq("premises_id",tbPremisesInfo.getId()));
            tbPremisesInfo.getAlbumList().forEach(a->{
                Album album = new Album().setPremisesId(tbPremisesInfo.getId()).setName(a.getName()).setUrl(a.getUrl());
                iAlbumService.saveOrUpdate(album);
            });
        }
        return true;
    }

    //修改回显
    @Override
    public PremisesInfo ByIdPremisesInfo(Integer id) {
        PremisesInfo premisesInfo = iPremisesInfoService.getById(id);
        System.err.println(premisesInfo);
        //建筑类型
        List<String> integers = new ArrayList<>();
        String buildingType = premisesInfo.getBuildingType();
        if (StringUtils.isNotEmpty(buildingType)){
            String[] split = buildingType.split(",");
            for (String s : split) {
                integers.add(s);
            }
            premisesInfo.setJianzhu(integers);
        }
        //社区
        List<String> she = new ArrayList<>();
        String communityType = premisesInfo.getCommunityType();
        if (StringUtils.isNotEmpty(communityType)){
            String[] split = communityType.split(",");
            for (String s : split) {
                she.add(s);
            }
            premisesInfo.setShequ(she);
        }
        //物业类型
        List<String> wu = new ArrayList<>();
        String premisesType = premisesInfo.getPremisesType();
        if (StringUtils.isNotEmpty(premisesType)){
            String[] split = premisesType.split(",");
            for (String s : split) {
                wu.add(s);
            }
            premisesInfo.setWuye(wu);
        }
        //商圈
        List<Integer> shang = new ArrayList<>();
        String sq = premisesInfo.getCommercialArea();
        if (StringUtils.isNotEmpty(sq)){
            String[] split = sq.split(",");
            for (String sss : split) {
                shang.add(Integer.parseInt(sss));
            }
            premisesInfo.setShangquan(shang);
        }
        //地铁
        ArrayList<Integer> integers1 = new ArrayList<>();
        String di = premisesInfo.getSubway();
        if (StringUtils.isNotEmpty(di)){
            String[] ditie = di.split(",");
            for (String s : ditie) {
                integers1.add(Integer.parseInt(s));
            }
            premisesInfo.setDitie(integers1);
        }
        //楼盘特色
        List<RelationPremisesProperty> premises_id = relationPremisesPropertyService.list(new QueryWrapper<RelationPremisesProperty>().eq("premises_id", premisesInfo.getId()));
        List<Integer> collect = premises_id.stream().map(a -> a.getPropertyId()).collect(Collectors.toList());
        System.err.println(collect);
        premisesInfo.setConfigProperty(collect);
        //追加行处理iPremisesFacilityService
        // 周边配套
        List<PremisesFacility> premisesFacilities = iPremisesFacilityService.list(new QueryWrapper<PremisesFacility>().eq("premises_id", premisesInfo.getId()).eq("type", 0));
        premisesInfo.setPeripheralFacilities(premisesFacilities);
        // 交通情况 List<PremisesFacility> trafficCondition;
        List<PremisesFacility> premisesFacilities1 = iPremisesFacilityService.list(new QueryWrapper<PremisesFacility>().eq("premises_id", premisesInfo.getId()).eq("type", 1));
        premisesInfo.setTrafficCondition(premisesFacilities1);
        //相册
        List<Album> albumList = iAlbumService.list(new QueryWrapper<Album>().eq("premises_id", premisesInfo.getId()));
        premisesInfo.setAlbumList(albumList);
        return premisesInfo;
    }
}
